<template>
	<view class="web-view-page">
		<web-view :src="pUrl" @message="webMessage"></web-view>
	</view>
</template>

<script>
import parseUrl from "@/utils/parseUrl.js";

export default {
	props: {
		src: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			pUrl: this.$props.src,
			shareObj: {
				title: '',
				path: '',
				imageUrl: ''
			},
			useCustomShare: false,
			isNeedShopLogin: false, // 是否需要处理商城的登录
		}
	},
	computed: {
		ip() {
			return this.$baseUrl
		}
	},
	onShow() {
		// 处理需要刷新的页面
		if (this.pUrl.includes('roc-rent-owner-h5/rent.html#/contract') ||
			this.pUrl.includes('roc-rent-owner-h5/rent.html#/intention')) {
			const urlArr = this.pUrl.split('reflesh')
			this.pUrl = urlArr[0] + '&reflesh=' + Date.now()
		}
	},
	onLoad(options) {
		// 小程序中使用wx.setStorageSync存储基础URL
		wx.setStorageSync('baseURL', this.ip)

		console.log('webViewPageOptions:', options);
		// 小程序中统一使用options获取参数
		let url = decodeURIComponent(options.url) || ''
		console.log("原始url:", url);

		this.isNeedShopLogin = options.shopLogin || false
		// url = decodeURIComponent(url)

		// 处理URL拼接
		let pUrl = /^https/.test(url) ? url : this.ip + url
		this.pUrl = pUrl = this.formatUrl(pUrl)
		console.log(pUrl, 'purl')

		// 调试环境替换域名
		// this.pUrl = pUrl.replace('http://console.wuyetoon.com', 'https://gfzc-sit.syswinsoft.com')
		// this.pUrl = this.getRealUrl()
		console.log('webview最终跳转url:', this.pUrl)

		// 小程序隐藏分享菜单
		if (this.pUrl.includes('visitInvite')) {
			wx.hideShareMenu({
				menus: ['shareAppMessage', 'shareTimeline'],
				success: () => {
					console.log('分享菜单已隐藏')
				},
				fail: (err) => {
					console.error('隐藏分享菜单失败:', err)
				}
			})
		}
	},
	methods: {
		/**
		 * 格式化URL，处理重复斜杠问题
		 */
		formatUrl(pUrl) {
			return pUrl.replace(/([^:]\/)\/+/g, "$1")
		},

		/**
		 * 从参数数组中获取指定参数值
		 */
		getParamFromArray(arr, name) {
			for (let i = 0; i < arr.length; i++) {
				if (arr[i].indexOf(name) > -1) {
					return arr[i].split('=')[1]
				}
			}
			return ''
		},

		/**
		 * 处理H5页面发送的消息
		 */
		webMessage(e) {
			console.log("收到H5消息:", e);

			if (!e.detail || !e.detail.data || e.detail.data.length === 0) {
				console.error('消息格式不正确')
				return
			}

			this.useCustomShare = true;
			this.shareObj = e.detail.data[e.detail.data.length - 1]

			let link = this.shareObj.link
			if (link && link.includes('/pages/jumppage/jumppage?toPage=roc-notice-owner-h5&pageType=registerPass')) {
				// 处理访客邀请链接
				const i = link.indexOf('?')
				if (i > -1) {
					const p = link.substring(i + 1)
					const arr = p.split('&')
					const pageType = this.getParamFromArray(arr, 'pageType')
					const id = this.getParamFromArray(arr, 'id')
					const toPage = this.getParamFromArray(arr, 'toPage')

					// 构建新URL
					const urlFK = `${this.ip}/${toPage}/index.html#/${pageType}?id=${id}`;
					this.pUrl = this.formatUrl(urlFK).replace('yztminipro.syswinsoft.com:9001', 'wxyztoon.syswinsoft.com')
					console.log('更新后的webview url:', this.pUrl);
				}
			}
		}
	},
	/**
	 * 小程序分享好友
	 */
	onShareAppMessage() {
		if (this.useCustomShare && this.shareObj) {
			console.log('使用自定义分享数据:', this.shareObj);
			return {
				title: this.shareObj.title || '慧聚邻',
				path: this.shareObj.path || '/pages/home/index',
				imageUrl: this.shareObj.imageUrl || '',
				success: () => {
					console.log('分享成功')
				},
				fail: (err) => {
					console.error('分享失败:', err)
				}
			}
		} else {
			return {
				title: '慧聚邻',
				path: '/pages/home/index',
				imageUrl: ''
			}
		}
	},
	/**
	 * 小程序分享到朋友圈
	 */
	onShareTimeline() {
		return {
			title: this.useCustomShare && this.shareObj.title ? this.shareObj.title : '慧聚邻',
			query: this.useCustomShare && this.shareObj.path ? this.shareObj.path.split('?')[1] || '' : '',
			imageUrl: this.useCustomShare && this.shareObj.imageUrl ? this.shareObj.imageUrl : ''
		}
	}
}
</script>

<style scoped lang="scss">
.web-view-page {
	height: 100vh;
	width: 100vw;
	overflow: hidden;
}
</style>
